package org.github.choviwu.web.index;

import org.apache.commons.lang.time.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.github.choviwu.example.common.token.MyUsernamePasswordToken;
import org.github.choviwu.example.common.util.JwtUtil;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ChoviWu on 2018/05/04
 * Description:
 */
@RestController
public class LoginController {

    private String key = "FSMKSMVISKIAID$23191293FEIWGJ1234!234FFAMKMPI";

    @PostMapping(value = "/user/login",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object login(String mobile,String password,String code) {

        if (mobile.equals("123") && password.equals("123")) {

            Date date = new Date();
            date = DateUtils.addMonths(date, 1);
            String token = JwtUtil.token(mobile, key, date);
            //Shiro
            UsernamePasswordToken usernamePasswordToken = new MyUsernamePasswordToken(mobile,password,0);
            Subject subject = SecurityUtils.getSubject();
            subject.login(usernamePasswordToken);
            Map map = new HashMap();
            map.put("token", token);
            map.put("mobile", mobile);
            map.put("password", password);
            return map;
        }
        return null;
//        return json;
    }

//    @GetMapping(value = "/validate")
//    public Object validate(HttpServletRequest request){
//        String path = HttpUtil.writeFile(request,ScoreUtil.IMAGE,".jpg");
//        return path;
//    }

}
